package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.NotificationDto;
import xyz.zhouzhaodong.crm.entity.Notification;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.mapper.NotificationMapper;
import xyz.zhouzhaodong.crm.service.INotificationService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 通知书202 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-27
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements INotificationService {

    @Resource
    NotificationMapper notificationMapper;

    @Resource
    CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addNotification(NotificationDto notificationDto) {
        try {
            Notification notification = notificationDtoToNotification(notificationDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            notification.setId(id);
            notification.setCreateUser(notificationDto.getStaffId());
            notification.setCreateTime(new Date());
            notificationMapper.insert(notification);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteNotification(NotificationDto notificationDto) {
        try {
            Notification notification = new Notification();
            notification.setId(notificationDto.getId());
            notification.setFlag(1);
            notification.setUpdateUser(notificationDto.getStaffId());
            notification.setUpdateTime(new Date());
            notificationMapper.updateById(notification);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateNotification(NotificationDto notificationDto) {
        try {
            Notification notification = notificationDtoToNotification(notificationDto);
            notification.setUpdateUser(notificationDto.getStaffId());
            notification.setUpdateTime(new Date());
            notificationMapper.updateById(notification);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectNotification(NotificationDto notificationDto) {
        Page<NotificationDto> page = new Page<>(notificationDto.getPageNo(),
                notificationDto.getPageSize() == 0 ? 10 : notificationDto.getPageSize());
        if (notificationDto.getFlag() == null) {
            notificationDto.setFlag(0);
        }
        IPage<NotificationDto> list = notificationMapper.selectListPage(page, notificationDto);
        for (NotificationDto n : list.getRecords()) {
            n.setCustomer(customerMapper.selectById(n.getCustomerId()));
        }
        return RestMessage.success(list);
    }

    /**
     * 实体类转化
     *
     * @param notificationDto
     * @return
     */
    private Notification notificationDtoToNotification(NotificationDto notificationDto) {
        Notification notification = new Notification();
        notification.setId(notificationDto.getId());
        notification.setCustomerId(notificationDto.getCustomerId());
        notification.setNotification(notificationDto.getNotification());
        notification.setTwoZeroTwo(notificationDto.getTwoZeroTwo());
        notification.setRemark(notificationDto.getRemark());
        notification.setFlag(notificationDto.getFlag());
        return notification;
    }

}
